<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Task
 *
 * @author QuangKhoi
 */
class Task {

    public $taskId;
    public $taskName;
    public $taskNote;
    public $taskUserId;
    public $taskAssignedTo;
    public $taskDateCreated;
    public $taskType;
    public $signalId;
    public $status;
    private $signal;
    private $tags;
    public $streamId;
    public $action;
    public $department;
    public $post;

    public function __constructor() {
        
    }

    public function __get($name) {
        if ($name == 'signal') {
            return Signal::withId($this->signalId);
        }
        if ($name == 'tags') {
            return Tag::loadTasksTag($this->taskId);
        }
        return $this->$name;
    }

    public static function loadTask($taskIds) {
        if (count($taskIds) == 0)
            return null;
        $db = Database::getInstance();
        $taskId = implode(',', $taskIds);
        $query = "SELECT * FROM tasks WHERE taskid IN ($taskId) ORDER BY taskdatecreated DESC";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow > 0) {
                $tasks = array();
                $messageIds = array();
                foreach ($result->dataTable->rows as $row) {
                    $task = new self();
                    $task->taskId = $row['taskid'];
                    $task->taskName = $row['taskname'];
                    $task->taskNote = $row['taskcontent'];
                    $task->taskUserId = $row['taskuserid'];
                    $task->taskAssignedTo = $row['taskassignedto'];
                    $task->taskDateCreated = new DateTime($row['taskdatecreated']);
                    $task->taskType = $row['tasktype'];
                    $task->signalId = $row['signalid'];
                    $task->status = $row['status'];
                    $task->assignedTo = User::withId($row['taskassignedto']);
                    $task->action = $row['action'];
                    $task->department = $row['department'];
                    $task->post = $row['post'];
                    $task->signal = Signal::withId($task->signalId);
                    $messageIds[] = $task->signal->messageID;
                    $tasks[] = $task;
                }/*
                $reader = new ZPostReader($messageIds, 7);
                $items = array();
                try {
                    $reader->read(function($item, $total) use (&$items) {
                        $items[] = $item;
                    });
                } catch (Exception $e) {

                }
                if (count($items))
                    foreach ($items as $message) {
                        foreach ($tasks as $task) {
                            if ($task->signal->messageID == $message->getID()) {
                                $task->signal->message = $message;
                                var_dump($task->signal);
                            }
                        }
                    }*/
                return $tasks;
            } else {
                $controller = new ErrorController(new Error(), new ErrorView(), "404", "Not found any task with provided id");
                $controller->returnError();
                return NULL;
            }
        } else {
            $controller = new ErrorController(new Error(), new ErrorView, $result->returnCode, $result->message);
            $controller->returnError();
            return NULL;
        }
    }

    public static function withId($id) {
        $instance = new self();
        $db = Database::getInstance();
        $result = $db->query("SELECT * FROM tasks WHERE taskid = '$id'");
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow == 1) {
                $row = $result->dataTable->rows[0];
                $instance->taskId = $row['taskid'];
                $instance->taskName = $row['taskname'];
                $instance->taskNote = $row['taskcontent'];
                $instance->taskUserId = $row['taskuserid'];
                $instance->taskAssignedTo = $row['taskassignedto'];
                $instance->taskDateCreated = new DateTime($row['taskdatecreated']);
                $instance->taskType = $row['tasktype'];
                $instance->signalId = $row['signalid'];
                $instance->status = $row['status'];
                $instance->post = $row['post'];
                $instance->action = $row['action'];
                $instance->department = $row['department'];
                return $instance;
            } else {
                $controller = new ErrorController(new Error(), new ErrorView(), "404", "Not found any user with provided email");
                $controller->returnError();
                return NULL;
            }
        } else {
            $controller = new ErrorController(new Error(), new ErrorView, $result->returnCode, $result->message);
            $controller->returnError();
            return NULL;
        }
    }

    /**
     * Construct a task with data
     * @param String $name
     * @param String $content
     * @param Int $userId
     * @param Int $assignedTo
     * @param Date $dateCreated
     * @param Int $type
     * @param Int $signalId
     */
    public static function withData($name, $content, $userId, $assignedTo, $dateCreated, $type, $signalId, $streamId, $post) {
        $instance = new self();
        $instance->taskId = "";
        $instance->taskName = $name;
        $instance->taskNote = $content;
        $instance->taskUserId = $userId;
        $instance->taskAssignedTo = $assignedTo;
        $instance->taskDateCreated = $dateCreated;
        $instance->taskType = $type;
        $instance->signalId = $signalId;
        $instance->status = 1;
        $instance->streamId = $streamId;
        $instance->post = $post;
        return $instance;
    }

    public function getTask($messageId) {
        $db = Database::getInstance();
        $userId = User::getCurrentUserId();
        $query = "SELECT * FROM tasks LEFT JOIN signals ON signals.signalid = tasks.signalid WHERE messageid = '$messageId' AND (userid = '$userId' OR taskassignedto = '$userId') AND tasktype < 10";
        $result = $db->query($query);
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow > 0) {
                $taskList = array();
                foreach ($result->dataTable->rows as $row) {
                    $instance = new self();
                    $instance->taskId = $row['taskid'];
                    $instance->taskName = $row['taskname'];
                    $instance->taskNote = $row['taskcontent'];
                    $instance->taskUserId = $row['taskuserid'];
                    $instance->taskAssignedTo = $row['taskassignedto'];
                    $instance->taskDateCreated = new DateTime($row['taskdatecreated']);
                    $instance->taskType = $row['tasktype'];
                    $instance->signalId = $row['signalid'];
                    $instance->status = $row['status'];
                    $instance->action = $row['action'];
                    $instance->post = $row['post'];
                    $instance->department = $row['department'];
                    $taskList[] = $instance;
                }
                return $taskList;
            }
        }
        return null;
    }
    
    public static function getTemplate($id){
        $result = Database::getInstance()->query("SELECT * FROM templates WHERE id='$id'");
        if($result->returnCode == 200){
            if($result->dataTable->numOfRow == 1){
                $row = $result->dataTable->rows[0];
                return $row;
            }
        }
    }
}

?>
